use fltk::{
    app::{self, App},
    prelude::*,
};
use fltk_theme::WidgetTheme;

use crate::ui::UserInterface;

#[derive(Debug, Clone, Copy)]
enum Message {
    Click,
}

pub struct MyApp {
    app: App,
    ui: UserInterface,
    receiver: app::Receiver<Message>,
}

impl MyApp {
    pub fn new() -> Self {
        let app = App::default();
        let widget_theme = WidgetTheme::new(fltk_theme::ThemeType::Metro);
        widget_theme.apply();
        let (sender, receiver) = app::channel::<Message>();
        let mut ui = UserInterface::make_window();
        ui.wind_main.set_label("{{project-name}}");
        ui.button_clickme.emit(sender.clone(), Message::Click);
        Self { app, ui, receiver }
    }

    pub fn launch(&mut self) {
        while self.app.wait() {
            use Message::*;
            if let Some(msg) = self.receiver.recv() {
                match msg {
                    Click => self.ui.button_clickme.set_label("Clicked"),
                }
            }
        }
    }
}
